package com.yzd.learnjava.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import com.yzd.learnjava.util.New;
import com.yzd.learnjava.util.Print;

public class GetChannel {
	public static void main(String[] args) throws IOException {
		FileChannel channel = new FileOutputStream("data.txt").getChannel();
		channel.write(ByteBuffer.wrap("Some text ".getBytes()));
		channel.close();
		
		channel = new RandomAccessFile("data.txt", "rw").getChannel();
		channel.position(channel.size());
		channel.write(ByteBuffer.wrap("Some more".getBytes()));
		channel.close();
		
		channel = new FileInputStream("data.txt").getChannel();
		ByteBuffer bf = ByteBuffer.allocate(1024);
		channel.read(bf);
		bf.flip();
		while(bf.hasRemaining()) {
			Print.printnb((char)bf.get());
		}
		
	}
}
